//# sourceURL=http://uMapApp/Module/PQI/PQI
define([
    "mhjs/mhjs"
    , "uMap/uMap"
    , "uEarth/uEarth"
    , "../../UI/Layer"
    , "../../Earth/ViewPort"
    , "../StakeNum/StakeNum"
], function (
    mhjs,
    uMap,
    uEarth,
    Layer,
    ViewPort,
    StakeNum
) {

    function PQI(option) {
        var _this = this;
        option && _this.OptionToProperties(option);

        // _this["数据集"] !== undefined && (_this["数据集"] = _this["数据集"].toFixed(0));

        _this.startStakeNum = StakeNum.transform(_this["开始桩号"]);
        _this.stopStakeNum = StakeNum.transform(_this["结束桩号"]);

        _this.stakeNum = _this.startStakeNum;

        _this.MQI !== undefined && (_this.MQI = _this.MQI.toFixed(2));
        _this.PQI !== undefined && (_this.PQI = _this.PQI.toFixed(2));
        _this.PCI !== undefined && (_this.PCI = _this.PCI.toFixed(2));
        _this.RQI !== undefined && (_this.RQI = _this.RQI.toFixed(2));
        _this.RDI !== undefined && (_this.RDI = _this.RDI.toFixed(2));
        _this.PBI !== undefined && (_this.PBI = _this.PBI.toFixed(2));
        _this.SRI !== undefined && (_this.SRI = _this.SRI.toFixed(2));
        _this.PSSI !== undefined && (_this.PSSI = _this.PSSI.toFixed(2));
        _this.SCI !== undefined && (_this.SCI = _this.SCI.toFixed(2));
        _this.BCI !== undefined && (_this.BCI = _this.BCI.toFixed(2));
        _this.TCI !== undefined && (_this.TCI = _this.TCI.toFixed(2));

        _this.colors = {
            normal   : ["#ff0", 0.01],
            hover    : ["#ff0", 0.25],
            active   : ["#ff0", 0.5],
            polyline : ["#FFF", 1]
        };


        if (_this["车道序号"]) {
            _this["车道"] = "第" + ["一", "二", "三"][_this["车道序号"] - 1] + "车道";
        } else {
            _this["车道"] = "整体";
        }

        _this.title = _this["朝向"] + " / " + _this["开始桩号"] + " - " + _this["结束桩号"] + " / " + _this["车道"];

        _this.searchKey = _this["朝向"] + " " + _this["开始桩号"] + " " + _this["结束桩号"] + " " + _this["车道"];

        _this.sortKey = (_this["朝向"] === "上行" ? 1e7 : 2e7) + _this.startStakeNum.distance + (_this["车道序号"] === undefined ? 0 : _this["车道序号"]) * 0.1;


        if (_this.positions) {

            _this.positions = new uMap.Positions(_this.positions);

            _this.layer = new Layer({
                "app"      : _this.app,
                "viewport" : new ViewPort({
                    "center" : _this.positions.positions[2],
                    "hpr"    : {
                        "heading" : uMap.Positions.getHeading(_this.positions.positions[0], _this.positions.positions[2]),
                        "pitch"   : -20,
                        "range"   : 600
                    }
                })
            });

            _this.entity = new uMap.Entity({
                objectOption : {
                    polyline : new uMap.PolylineGraphic({
                        objectOption : {
                            positions     : _this.positions.positions.concat([_this.positions.positions[0]]),
                            clampToGround : true,
                            width         : 2,
                            color         : _this.colors.polyline
                        }
                    }),
                    polygon  : new uMap.PolygonGraphic({
                        objectOption : {
                            positions : {positions : _this.positions},
                            color     : _this.colors.normal
                            // , outlineColor: ["#ff0"]
                            // , extrudedHeight: 1
                            // , extrudedHeightReference: uEarth.HeightReference.RELATIVE_TO_GROUND
                        }
                    })
                }
            });

            _this.entity.on("click", function (event) {
                _this.trigger("click", event);
            });

            _this.layer.dataSources.entities.add(_this.entity.object);


            // _this.classification = new uMap.ModelClassificationPrimitive({
            //     objectOption: {
            //         geometry  : new uEarth.PolygonGeometry({
            //             polygonHierarchy: new uEarth.PolygonHierarchy(_this.positions.cartesian),
            //             extrudedHeight  : 5000,
            //             height          : 0
            //         }),
            //         attributes: {
            //             "color": uEarth.ColorGeometryInstanceAttribute.fromColor(
            //                 new uMap.Color(["#f00", 0.5])
            //             )
            //         }
            //     }
            // });

            // _this.layer.primitives.add(_this.classification.object);

        }


    }

    var _class = PQI;

    mhjs.OptionToPropertiesInterface(_class);
    mhjs.EventInterface(_class);

    Object.defineProperties(_class.prototype, {
        "open"   : {
            value : function () {
                if (!this.isOpened) {
                    this.layer && this.layer.open();
                    this.isOpened = true;
                    this.trigger("open");
                }
            }
        },
        "close"  : {
            value : function () {
                if (this.isOpened) {
                    this.layer && this.layer.close();
                    this.isOpened = false;
                    this.trigger("close");
                }
            }
        },
        "toggle" : {
            value : function () {
                if (this.isOpened) {
                    this.close();
                } else {
                    this.open();
                }
            }
        },
        "active" : {
            value : function (isActive) {
                var _this = this;

                isActive = (isActive === undefined) || Boolean(isActive);
                _this.isActive = _this.isActive === undefined ? false : _this.isActive;

                if (isActive !== _this.isActive) {

                    if (isActive) {

                        if (_this.layer) {
                            _this.entity.polygon.material.color = new uMap.Color(_this.colors.active);
                        }

                        _this.trigger("active");

                    } else {

                        if (_this.layer) {
                            _this.entity.polygon.material.color = new uMap.Color(_this.colors.normal);
                        }

                        _this.trigger("unActive");
                    }

                    _this.isActive = isActive;

                }


            }
        }
    });

    return _class;

});